// const Cc = Components.classes;
// const Ci = Components.interfaces;
// const Cr = Components.results;
// const Cu = Components.utils;
// const loader = Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader);
// loader.loadSubScript("chrome://antiyouku/content/fileult.js");
// loader.loadSubScript("chrome://antiyouku/content/ult.js");
// loader.loadSubScript("chrome://antiyouku/content/TracingListener.js");
	
var httpRequestObserver =
{ 
  observe: function(subject, topic, data) {
//    if (topic == "http-on-modify-request") {
//
//        var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);     
//        var requestURL = subject.URI.spec;
//
//        if (requestURL.search(/http:\/\/static\.youku\.com(\/v[\d\.]*)?\/v\/swf\/player.*\.swf/) >= 0)  {
//        	filecontent = ulttool.loadbytefile('player.swf');
//        	httpChannel.setResponseHeader("Content-Length",filecontent.length,true);
//        	ulttool.loginfo('http-on-modify-request Content-Length:'+httpChannel.getResponseHeader("Content-Length"));
//        }
//    }
    
    if (topic == "http-on-examine-response") {
		 
//        ulttool.loginfo('httpChannel.getResponseHeader("Content-Length"):'+httpChannel.getResponseHeader("Content-Length"));
		try{
			requestURL = subject.URI.spec;
			if (requestURL.search(/http:\/\/static\.youku\.com(\/v[\d\.]*)?\/v\/swf\/player.*\.swf/) >= 0){
				var newListener = new TracingListener();
		        var traceableChannel = subject.QueryInterface(Ci.nsITraceableChannel);
		        newListener.originalListener = traceableChannel.setNewListener(newListener);  
		        var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
        
				newListener.reqlength = parseInt(httpChannel.getResponseHeader("Content-Length"));
				newListener.filecontent = ulttool.loadbytefile(filename);
				
//				httpChannel.setResponseHeader("Content-Length",filecontent.length,true);
			}
        }catch(err){
        }
    }

  },
  
  isToBeReplaced: function(requestURL) {
	
	if(requestURL.search(/crossdomain\.xml/ig)>=0){
		ulttool.loginfo('-------requestURL:'+requestURL);
		if (requestURL=='http://v.youku.com/crossdomain.xml'){
			return false;
		}
		return false;
	}else{
		return false;
	}
	
  },

  get observerService() {
    return Components.classes["@mozilla.org/observer-service;1"]
                     .getService(Components.interfaces.nsIObserverService);
  },

  register: function() {
//		this.observerService.addObserver(this, "http-on-modify-request", false);
		this.observerService.addObserver(this, "http-on-examine-response", false);
  },
  
  unregister: function() {
//		this.observerService.removeObserver(this, "http-on-modify-request");
		this.observerService.removeObserver(this, "http-on-examine-respons");
  }
};

httpRequestObserver.register();